using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerStats : CharacterStats
{
    private Player player;

    #region Emotion Info    (��������)
    public float currentDelight;
    public float currentRage;
    public float currentGrief;
    public float currentFear;

    public Dictionary<EmotionType, Stat> maxEmotionValueDic;
    public Dictionary<EmotionType, float> currentEmotionValueDic;

    //public Stat delightGetByAtk;
    //public Stat rageGetByAtk;
    //public Stat griefGetByAtk;
    //public Stat fearGetByAtk;

    public Dictionary<EmotionType, Stat> emotionValueGetByAtkDic;

    public Stat delightLoseByAtk;
    public Stat rageLoseByAtk;
    public Stat griefLoseByAtk;
    public Stat fearLoseByAtk;

    public Stat delightLoseSpeed;
    public Stat rageLoseSpeed;
    public Stat griefLoseSpeed;
    public Stat fearLoseSpeed; 

    public System.Action onDelightChanged;
    public System.Action onRageChanged;
    public System.Action onGriefChanged;
    public System.Action onFearChanged;
    public System.Action onEmotionChanged;
    #endregion

    protected override void Start()
    {
        base.Start();

        player = GetComponent<Player>();

        maxEmotionValueDic = new Dictionary<EmotionType, Stat>();
        currentEmotionValueDic = new Dictionary<EmotionType, float>();
        emotionValueGetByAtkDic = new Dictionary<EmotionType, Stat>();

        SetMaxEmotionValueDic(EmotionType.Delight);
        SetMaxEmotionValueDic(EmotionType.Rage);
        SetMaxEmotionValueDic(EmotionType.Grief);
        SetMaxEmotionValueDic(EmotionType.Fear);

        SetCurrentEmotionValueDic(EmotionType.Delight);
        SetCurrentEmotionValueDic(EmotionType.Rage);
        SetCurrentEmotionValueDic(EmotionType.Grief);
        SetCurrentEmotionValueDic(EmotionType.Fear);
    }

    #region HP And Damage   (Ѫ�����˺�)
    protected override void DecreaseHP(float _damaged)
    {
        base.DecreaseHP(_damaged);

        //ItemData_Euqipment currentArmor = Inventory.instance.GetEquipment(EquipmentType.Armor);

        //if (currentArmor != null)
            //currentArmor.Effect(player.transform);
    }

    protected override void Die()
    {
        base.Die();

        player.Die();
    }
    #endregion

    #region EmotionValue

    public void GetEmotion()        //��ȡ����ֵ
    {
        ItemData_Weapon weapon = Inventory.instance.weaponInEquipment[PlayerManager.instance.player.weaponCount];

        //if (weapon.data.emotionType == EmotionType.Delight) IncreaseDelight();
        //if (weapon.data.emotionType == EmotionType.Rage) IncreaseRage();
        //if (weapon.data.emotionType == EmotionType.Grief) IncreaseGrief();
        //if (weapon.data.emotionType == EmotionType.Fear) IncreaseFear();

        IncreaseEmotion(weapon.emotionType);
    }

    private void IncreaseEmotion(EmotionType emotionType)
    {
        float maxEmotionValue = maxEmotionValueDic[emotionType].GetValue();
        float currentEmotionValue = currentEmotionValueDic[emotionType];
        float emotionValueGetByAtk = PlayerManager.instance.player.currentWeapon.emotionGetByAtk.GetValue();
        Debug.Log(emotionValueGetByAtk);

        if (currentEmotionValue + emotionValueGetByAtk < maxEmotionValue)
            currentEmotionValueDic[emotionType] += emotionValueGetByAtk;
        else
            currentEmotionValueDic[emotionType] = maxEmotionValue;

        if (onEmotionChanged != null)
            onEmotionChanged();
    }

    private void SetMaxEmotionValueDic(EmotionType emotionType)
    {
        if (!maxEmotionValueDic.ContainsKey(emotionType))
            maxEmotionValueDic.Add(emotionType, GetMaxEmotionValue(emotionType));
        else
            maxEmotionValueDic[emotionType] = GetMaxEmotionValue(emotionType);
    }

    private void SetCurrentEmotionValueDic(EmotionType emotionType)
    {
        if (!currentEmotionValueDic.ContainsKey(emotionType))
            currentEmotionValueDic.Add(emotionType, GetCurrentEmotionValue(emotionType));
        else 
            currentEmotionValueDic[emotionType] = GetCurrentEmotionValue(emotionType);
    }

    private Stat GetMaxEmotionValue(EmotionType emotionType)
    {
        if (emotionType == EmotionType.Delight) return maxDelight;
        else if(emotionType == EmotionType.Rage) return maxRage;
        else if(emotionType == EmotionType.Grief) return maxGrief;
        else if(emotionType == EmotionType.Fear) return maxFear;
        return null;
    }

    private float GetCurrentEmotionValue(EmotionType emotionType)
    {
        if (emotionType == EmotionType.Delight) return currentDelight;
        else if (emotionType == EmotionType.Rage)    return currentRage;
        else if (emotionType == EmotionType.Grief)   return currentGrief;
        else if (emotionType == EmotionType.Fear)    return currentFear;
        return 0;
    }
    #endregion

    #region Old Function
    //private void IncreaseDelight()  //��������ֵ��ϲ
    //{
    //    if(currentDelight + delightGetByAtk.GetValue() < maxDelight.GetValue())
    //        currentDelight += delightGetByAtk.GetValue();
    //    else
    //        currentDelight = maxDelight.GetValue();

    //    if (onDelightChanged != null)
    //        onDelightChanged();
    //}

    //private void IncreaseRage()     //��������ֵ��ŭ
    //{
    //    if(currentRage + rageGetByAtk.GetValue() < maxRage.GetValue())
    //        currentRage += rageGetByAtk.GetValue();
    //    else
    //        currentRage = maxRage.GetValue();

    //    if(onRageChanged != null)
    //        onRageChanged();
    //}

    //private void IncreaseGrief()    //��������ֵ����
    //{
    //   if(currentGrief + griefGetByAtk.GetValue() < maxGrief.GetValue())
    //        currentGrief += griefGetByAtk.GetValue();
    //   else
    //        currentGrief = maxGrief.GetValue();

    //    if(onGriefChanged != null)
    //        onGriefChanged();
    //}

    //private void IncreaseFear()     //��������ֵ����
    //{
    //    if(currentFear + fearGetByAtk.GetValue() < maxFear.GetValue())
    //        currentFear += fearGetByAtk.GetValue();
    //    else
    //        currentFear = maxFear.GetValue();

    //    if(onFearChanged != null) 
    //        onFearChanged();
    //}
    #endregion

    protected virtual void SetEmotionEffect(EmotionType _emotionType)
    {

    }

    protected virtual void ExecuteDelightEffect()
    {
        if (currentDelight > 0)
        {

        }
    }

    protected virtual void ExecuteRageEffect() { }

    protected virtual void ExecuteGriefEffect() { }

    protected virtual void ExecuteFearEffect() { }
}
